package com.cop.consultapp.parser;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.cop.consultapp.model.QuestionVO;

public class ConsultParser extends DefaultHandler {
	
	private static String ITEMS = "ITEMS";
	private static String ITEM = "ITEM";
	
	private static String IDX = "IDX";	
	private static String TELNO	= "TELNO";
	private static String TITLE = "TITLE";
	private static String NAME = "NAME";
	private static String CONTENT = "CONTENT";
	
	private String currentName="";
	
	private QuestionVO questionVO = null;
	private ArrayList<QuestionVO> questionList = null;
	
	
	public ArrayList<QuestionVO> getQuestionList() {
		return questionList;
	}

	public void setQuestionList(ArrayList<QuestionVO> rankList) {
		this.questionList = questionList;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		this.currentName = localName;
		
		if(localName.equals(this.ITEM)){
			this.questionVO = new QuestionVO();
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		
		super.characters(ch, start, length);
		
		String value = new String(ch, start, length);
		
		if(this.IDX.equals(this.currentName)) {
			this.questionVO.setIdx(Integer.parseInt(value));	
		}else if(this.NAME.equals(this.currentName)) {
			this.questionVO.setName(value);	
		}else if(this.TELNO.equals(this.currentName)) {
			this.questionVO.setTelno(value);
		}else if(this.TITLE.equals(this.currentName)) {
			this.questionVO.setTitle(value);
		}else if(this.CONTENT.equals(this.currentName)) {
			this.questionVO.setContent(value);
			
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		
		if(this.ITEM.equals(localName)) {
			
			if(this.questionList==null) this.questionList = new ArrayList<QuestionVO>();
			
			this.questionList.add(questionVO);
			questionVO = null;			
		}
		
		this.currentName = null;
	}

	
	
	

}
